package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    //查询菜品信息
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,String name
    ){
        //1.调用Service分页查询
        Page<Dish> page = dishService.findByPage(name,pageNum,pageSize);
        //2. 返回结果
        return ResultInfo.success(page);
    }

    //查询菜品分类
    @GetMapping("/category/list")
    public ResultInfo findByType(Integer type){
        List<Category> categoryList = dishService.findByType(type);
        return ResultInfo.success(categoryList);
    }

    //保存菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){
        //执行菜品删除方法的时候,要对应的删除redis中所有菜品的缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        dishService.save(dish);
        return ResultInfo.success(null);
    }

    //回显菜品信息
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") long id){
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    //修改菜品
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        //执行菜品删除方法的时候,要对应的删除redis中所有菜品的缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishService.update(dish);

        return ResultInfo.success(null);
    }

    //修改菜品状态
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(
            @PathVariable("status") Integer status,
            long[] ids
    ){
        //执行菜品删除方法的时候,要对应的删除redis中所有菜品的缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        if(ids != null && ids.length>0) {
            dishService.updateStatus(status, ids);
       /* for (long id : ids) {//在这里for循环 可能因为停电等中断  没有事务判断  所以不行
          dishService.updateStatus(status,id);
        }*/
        }
        return ResultInfo.success(null);
    }

    //删除菜品
    @DeleteMapping("/dish")
    public ResultInfo delete(Long[] ids){
        //执行菜品删除方法的时候,要对应的删除redis中所有菜品的缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        if(ids != null && ids.length>0) {
            dishService.delete(ids);
        }
        return ResultInfo.success(null);
    }

   /* //根据分类id查询菜品
    @GetMapping("/dish/list")
    public ResultInfo findByCategoryId(@RequestBody Map<String,String> map){
        String id = map.get("categoryId");
        String name = map.get("name");
        Long categoryId = Long.parseLong(id);
        if(name != null){
            List<Dish> dishList = dishService.findByName(name);
            return ResultInfo.success(dishList);
        }else {
            List<Dish> dishList = dishService.findByCategoryId(categoryId);
            return ResultInfo.success(dishList);
        }
    }*/

    //根据名字查菜品
    @GetMapping("/dish/list")
    public ResultInfo findByCategoryId(String name,Long categoryId){
        List<Dish> dishList = dishService.findByCategoryId(name,categoryId);
        return ResultInfo.success(dishList);
    }
}



